from datetime import datetime
from DjangoUeditor.models import UEditorField
from django.db import models


class City(models.Model):
    """
    机构城市
    """
    name = models.CharField(verbose_name=u"城市名称", max_length=20)
    desc = UEditorField(verbose_name=u"城市介绍", width=800, height=300, default="",
                          imagePath="course/ueditor/%(basename)s_%(datetime)s.%(extname)s",
                          filePath ="course/ueditor/%(basename)s_%(datetime)s.%(extname)s")
    addTime = models.DateTimeField(verbose_name=u"添加时间", default=datetime.now)

    class Meta:
        verbose_name = u"机构城市"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Org(models.Model):
    """
    机构
    """
    # 城市与机构属于一对多的关系
    city = models.ForeignKey(City,verbose_name=u"机构城市")
    name = models.CharField(verbose_name=u"机构名称",max_length=50)
    desc = UEditorField(verbose_name=u"机构描述", width=800, height=300,
                        imagePath="course/ueditor/%(basename)s_%(datetime)s.%(extname)s",
                        filePath="course/ueditor/%(basename)s_%(datetime)s.%(extname)s")
    category = models.CharField(verbose_name=u"机构类型", max_length=5, choices=(("px","培训机构"),("gx","高校")),default="px")
    image = models.ImageField(verbose_name=u"封面图", max_length=100, upload_to="org/%Y/%m/%d")
    address = models.CharField(verbose_name=u"地址", max_length=150)
    addTime = models.DateTimeField(verbose_name=u"添加时间", default=datetime.now)
    clickNum = models.IntegerField(verbose_name=u"点击数", default=0)
    favNum = models.IntegerField(verbose_name=u"收藏数", default=0)
    students = models.IntegerField(verbose_name=u"学习人数", default=0)
    courses = models.IntegerField(verbose_name=u"课程数", default=0)

    class Meta:
        verbose_name = u"机构信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    # 机构课程信息
    def getOrgCourse(self):
        return self.course_set.all()

    # 机构教师信息
    def getOrgTeacher(self):
        return self.teacher_set.all()



class Teacher(models.Model):
    """
    机构教师
    """
    # 机构与教师一对多的关系
    org = models.ForeignKey(Org,verbose_name=u"机构")
    name = models.CharField(verbose_name=u"教师名",max_length=50)
    age = models.IntegerField(verbose_name=u"年龄",default=18)
    images = models.ImageField(verbose_name=u"头像",upload_to="HeadPortrait/teacher/%Y/%m/%d")
    workYear = models.IntegerField(verbose_name="就职年限",default=0)
    workCompany = models.CharField(verbose_name=u"就职单位",max_length=50)
    workPosition = models.CharField(verbose_name=u"就职地点",max_length=100)
    points = models.CharField(verbose_name=u"特长",max_length=50)
    clickNum = models.IntegerField(verbose_name=u"点击数", default=0)
    favNum = models.IntegerField(verbose_name=u"收藏数", default=0)
    addTime = models.DateTimeField(verbose_name=u"添加时间",default=datetime.now)

    class Meta:
        verbose_name = u"机构讲师"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

    # 获取教师相关的课程
    def getTeacherCourses(self):
        return self.course_set.all()